<?php
namespace app\admin\model\myshuju\bind;
use app\admin\builder\Builder;
use app\admin\model\myshuju\myconfigs\MyConFig;
use think\facade\Db;

class BuliderToForm extends MyConFig
{
    public $data = [];
    public $field = [];
    public $paichu = [];
    public $fieldlimit = [];
    public $builder = '';
    public $action = '';
    public function __construct()
    {
        $this->builder = Builder::form();
        return $this;
    }

    /**添加表单跳转地
     * @param $str
     * @return $this
     */
    public function addFormAction($str){
        $this->action = $str;
        return $this;
    }

    /**添加按钮
     * @param array $array
     * @return $this
     */
    public function addFormButton($array=[]){
        foreach ($array as $value){
            $this->builder->setButton($value['type'],$value['desc']??null,$value['action']??null,$value['class']??null,$value['style']??null);
        }
        return $this;
    }

    /**添加form字段和不显示字段
     * @param array $field
     * @param array $paichu
     * @return $this
     */
    public function PaiChu($paichu=[]){
        $this->paichu = $paichu;
        return $this;
    }

    /**设置某字段限制
     * @param string $field
     * @param array $array
     */
    public function setFieldLimit($field='',$array=[]){
        if(!empty($field)){
            $this->fieldlimit[$field] = $array;
        }
        return $this;
    }


    /**加载form表单
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function fetch(){
        if(!$this->action){
            $this->action = request()->controller().'/add';
        }
        if(!$this->cid){
            $this->action = $this->action.'?model_id='.$this->model_id;
        }
//        echo $this->action;exit;
        $this->builder = $this->builder->setAction($this->action);
        if($this->id){
            $this->data = $this->getData();
        }
        if($this->data['id']??null){
            $this->builder = $this->builder->setProject('none','id','id',$this->data['id']);
        }
//        if($this->pid){
//            $this->builder = $this->builder->setProject('none','pid','pid',$this->pid);
//        }
//        echo '<pre>';
//        print_r($this->field);
//        exit;
        foreach ($this->field as $value){
            if($value['field']=='cid'){
                $this->builder = $this->builder->setProject('none','cid','栏目',$this->cid);
                continue;
            }
            if(!$value['edit']||in_array($value['field'],$this->paichu)){
                if($this->data){
                    $this->builder = $this->builder->setProject('none',$value['field'],$value['field_name'],$this->data[$value['field']]);
                }else{
                    $this->builder = $this->builder->setProject('none',$value['field'],$value['field_name'],$value['defaults']);
                }
                continue;
            }
            if($value['field']=='pid'){
                if(!$this->cid){
                    $array = Db::name($this->table)->where('status',1)->select()->toArray();
                    if($this->data){
                        $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs'],true);
                    }else{
                        if($this->pid){
                            $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$this->pid,$array,$value['descs'],true);
                        }else{
                            $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                        }
                    }
                    continue;
                }
            }
            if($value['form_type']=='单行文本'){
                if($this->data){
                    $this->builder = $this->builder->setProject('input',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('input',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='联动单行文本'){
                if($this->data){
                    if($this->data[$value['field']]){
                        $table = Db::name('models')->where('id',$value['table'])->find();
                        if($table){
                            $array = Db::name($table['table'])->where('id',$this->data[$value['field']])->find();
                            if($array){
                                $op = 'name';
                                if(!empty($value['option'])){
                                    $op = $value['option'];
                                }
                                $this->builder = $this->builder->setProject('input',$value['field'],$value['field_name'],$array[$op],'',$value['descs']);
                            }else{
                                $this->builder = $this->builder->setProject('input',$value['field'],$value['field_name'],'','',$value['descs']);
                            }
                        }else{
                            $this->builder = $this->builder->setProject('input',$value['field'],$value['field_name'],'','',$value['descs']);
                        }
                    }else{
                        $this->builder = $this->builder->setProject('input',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                    }
                }else{
                    $this->builder = $this->builder->setProject('input',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='无限联动框'){
                $table = Db::name(getTable('models'))
                    ->where('id',$value['table'])
                    ->find();
                if($value['hierarchy']){
                    $pid =  $value['hierarchy'];
                }else{
                    $pid = 'pid';
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('inclass',$value['field'],$value['field_name'],$this->data[$value['field']],$pid,$value['descs'],'',request()->controller().'/getchild?table='.$table['table'],null,null,$value['table']);
                }else{
                    $this->builder = $this->builder->setProject('inclass',$value['field'],$value['field_name'],$value['defaults'],$pid,$value['descs'],'',request()->controller().'/getchild?table='.$table['table'],null,null,$value['table']);
                }
            }
            if($value['form_type']=='多行文本'){
                if($this->data){
                    $this->builder = $this->builder->setProject('textarea',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('textarea',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='单文件'){
                if($this->data){
                    $this->builder = $this->builder->setProject('file',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('file',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='多文件'){
                if($this->data){
                    $this->builder = $this->builder->setProject('files',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('files',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='单图片'){
                if($this->data){
                    $this->builder = $this->builder->setProject('img',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('img',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='多图片'){
                if($this->data){
                    $this->builder = $this->builder->setProject('imgs',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('imgs',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='视频'){
                if($this->data){
                    $this->builder = $this->builder->setProject('video',$value['field'],$value['field_name'],$this->data[$value['field']],'',$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('video',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='单选框'){
                $array = explode(',',$value['option']);
//                if($value['field']=='pattern'){
////                    echo '<pre>';
////                    print_r($this->data);
////                    exit;
//                }
                if($this->data){
                    $this->builder = $this->builder->setProject('radio',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
            if($value['form_type']=='多选框'){
                $array = explode(',',$value['option']);
                if($this->data){
                    $this->builder = $this->builder->setProject('checkbox',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
            if($value['form_type']=='下拉框'){
                $array = explode(',',$value['option']);
                if($this->data){
                    $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
            if($value['form_type']=='联动单选框'){
                $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                $array = [];
                if($mytable){
                    if(isset($this->fieldlimit[$value['field']])){
                        $array = Db::name($mytable['table'])
                            ->where('id','in',$this->fieldlimit[$value['field']])
                            ->where('status',1)->select()->toArray();
                    }else{
                        $array = Db::name($mytable['table'])->where('status',1)->select()->toArray();
                    }
                }
                foreach ($array as $keys=>$valu){
                    if($value['option']){
                        $valu['name'] = $valu[$value['option']];
                    }
                    $array[$keys] = $valu;
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('radio',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs'],true);
                }else{
                    $this->builder = $this->builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                }
            }
            if($value['form_type']=='联动多选框'){
                $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                $array = [];
                if($mytable){
                    if($mytable==getTable('models')){
                        if(isset($this->fieldlimit[$value['field']])){
                            $array = Db::name($mytable['table'])->where('status',1)
                                ->where('id','in',$this->fieldlimit[$value['field']])
                                ->where('type','<>',1)->select()->toArray();
                        }else{
                            $array = Db::name($mytable['table'])->where('status',1)->where('type','<>',1)->select()->toArray();
                        }
                    }else{
                        if(isset($this->fieldlimit[$value['field']])){
                            $array = Db::name($mytable['table'])
                                ->where('id','in',$this->fieldlimit[$value['field']])
                                ->where('status',1)->select()->toArray();
                        }else{
                            $array = Db::name($mytable['table'])->where('status',1)->select()->toArray();
                        }
                    }
                }
                foreach ($array as $keys=>$valu){
                    if($value['option']){
                        $valu['name'] = $valu[$value['option']];
                    }
                    $array[$keys] = $valu;
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('checkbox',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs'],true);
                }else{
                    $this->builder = $this->builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                }
            }
            if($value['form_type']=='联动下拉框'){
                $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
//                echo '<pre>';
//                print_r($value);
//                exit;
                $array = [];
                if($mytable){
                    if($mytable['table']==getTable('admin_user')){
                        if(isset($this->fieldlimit[$value['field']])){
                            $array = Db::name($mytable['table'])
                                ->where('status',1)
                                ->where('id','in',$this->fieldlimit[$value['field']])
                                ->where('name','<>','root')
                                ->select()->toArray();
                        }else{
                            $array = Db::name($mytable['table'])
                                ->where('status',1)
                                ->where('name','<>','root')
                                ->select()->toArray();
                        }
                    }else{
                        if(isset($this->fieldlimit[$value['field']])){
//                            echo '<pre>';
//                            print_r($mytable['table']);
//                            exit;
                            $array = Db::name($mytable['table'])
                                ->where('status',1)
                                ->where('id','in',$this->fieldlimit[$value['field']])
                                ->select()->toArray();
                        }else{
                            $array = Db::name($mytable['table'])
                                ->where('status',1)
                                ->select()->toArray();
                        }
                    }
                }
                foreach ($array as $keys=>$valu){
                    if($value['option']){
                        $valu['name'] = $valu[$value['option']];
                    }
                    $array[$keys] = $valu;
                }
//                echo '<pre>';
//                print_r($array);
//                exit;
                if($this->data){
                    $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs'],true);
                }else{
                    $this->builder = $this->builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                }
            }
            if($value['form_type']=='联动下拉组'){
                $tablesid =  explode(',',$value['table']);
                $table1 = Db::name(getTable('models'))->where('id',$tablesid[0])->find();
                if($tablesid[1]??null){
                    $table2 = Db::name(getTable('models'))->where('id',$tablesid[1])->find();
                }else{
                    echo '联动下拉组最少联两表';
                    exit;
                }
                $array = Db::name($table1['table'])->where('status',1)->field('id,name')->select()->toArray();

                foreach ($array as $key=>$va){
                    $va['child'] = Db::name($table2['table'])->where('status',1)->where($value['hierarchy'],$va['id'])->field('id,name')->select()->toArray();
                    $array[$key] = $va;
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('optgroup',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs'],true);
                }else{
                    $this->builder = $this->builder->setProject('optgroup',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                }
            }
            if($value['form_type']=='编辑框'){
                if($this->data){
                    $this->builder = $this->builder->setProject('editor',$value['field'],$value['field_name'],$this->data[$value['field']],[],$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('editor',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                }
            }
            if($value['form_type']=='编辑框(layui)'){
                if($this->data){
                    $this->builder = $this->builder->setProject('editors',$value['field'],$value['field_name'],$this->data[$value['field']],[],$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('editors',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                }
            }
            if($value['form_type']=='开关'){
                if($value['option']){
                    $array = explode(',',$value['option']);
                }else{
                    $array = ['开启','关闭'];
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('switch',$value['field'],$value['field_name'],$this->data[$value['field']],$array,$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('switch',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
            if($value['form_type']=='时间'){
                if($this->data){
                    $this->builder = $this->builder->setProject('time',$value['field'],$value['field_name'],$this->data[$value['field']],['开启','关闭'],$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('time',$value['field'],$value['field_name'],time(),['开启','关闭'],$value['descs']);
                }
            }
            if($value['form_type']=='json数组'){
                if($value['option']){
                    $array = explode(':',$value['option']);
                }else{
                    $array = [
                        '属性',
                        '值'
                    ];
                }
                if($this->data){
                    $this->builder = $this->builder->setProject('json',$value['field'],$value['field_name'],json_decode($this->data[$value['field']],true),$array,$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('json',$value['field'],$value['field_name'],json_decode($value['defaults'],true),$array,$value['descs']);
                }
            }
            if($value['form_type']=='颜色选择框'){
                if($this->data){
                    $this->builder = $this->builder->setProject('color',$value['field'],$value['field_name'],$this->data[$value['field']],[],$value['descs']);
                }else{
                    $this->builder = $this->builder->setProject('color',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                }
            }
        }
        return $this->builder->fetch();
    }
}